#include "serialportprotocol.h"
#include "serialportdriver.h"
#include <QDebug>
#include "datatemp.h"
SerialPortProtocol *SerialPortProtocol::obj=nullptr;


SerialPortProtocol::SerialPortProtocol(QObject *parent) : QObject(parent)
{
connect(SerialPortDriver::getObject(),
        &SerialPortDriver::serialPortReadData,
        this,
        &SerialPortProtocol::serialPortReadDataSlot);//绑定信号与槽函数，信号来自driver类的getobject单例
}

SerialPortProtocol *SerialPortProtocol::getObject()
{
if(obj == nullptr)
    obj =new SerialPortProtocol;
return obj;
}




bool SerialPortProtocol::requestTem(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 08 5B 04 FF FF 01 01 02 10 01 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 08 6B 04 FF FF 01 01 02 20 01 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,0);

}

bool SerialPortProtocol::requestHum1(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 08 58 04 FF FF 01 01 02 10 02 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 08 68 04 FF FF 01 01 02 20 02 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,0);
}

bool SerialPortProtocol::requestLight(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 08 59 04 FF FF 01 01 02 10 03 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 08 69 04 FF FF 01 01 02 20 03 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,0);
}

bool SerialPortProtocol::requestPress(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 08 5E 04 FF FF 01 01 02 10 04 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 08 6E 04 FF FF 01 01 02 20 04 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,0);
}

bool SerialPortProtocol::requestHum2(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 08 5F 04 FF FF 01 01 02 10 05 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 08 6F 04 FF FF 01 01 02 20 05 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,0);
}

bool SerialPortProtocol::openWater1(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 09 5D 04 FF FF 01 01 03 10 06 01 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 09 6D 04 FF FF 01 01 03 20 06 01 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,1);
}

bool SerialPortProtocol::closeWater1(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 09 5C 04 FF FF 01 01 03 10 06 00 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 09 6C 04 FF FF 01 01 03 20 06 00 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,1);
}

bool SerialPortProtocol::openWater2(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 09 5C 04 FF FF 01 01 03 10 07 01 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 09 6C 04 FF FF 01 01 03 20 07 01 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,1);
}

bool SerialPortProtocol::closeWater2(int boardId)
{
    QByteArray data;
    if(boardId == 1)
    data.append(QByteArray::fromHex("55 00 44 00 09 5D 04 FF FF 01 01 03 10 07 00 AA"));
    else if(boardId == 2)
    data.append(QByteArray::fromHex("55 00 44 00 09 6D 04 FF FF 01 01 03 20 07 00 AA"));
    return SerialPortDriver::getObject()->serialPortWrite(data,1);
}


void SerialPortProtocol::serialPortReadDataSlot(const QByteArray &data)
{

 dataTemp.append(data);
 qDebug()<< "data=="<<dataTemp;
 int dataLength = dataTemp.length();
 if(dataLength < 9)//没有小于9的数据长度，说明数据出错
    {
     qDebug()<< "小于9";
     return;}
 if(dataLength > 100)//数据出错
 {
     dataTemp.clear();//清空数据
     qDebug()<< "大于100";
     return;

 }

for(int i=1;i<dataTemp.length();i++)
{

if(((unsigned char)dataTemp.at(i) == 0XFF ) && ((unsigned char)dataTemp.at(i-1) == 0X55)&&(unsigned char)dataTemp.at(i+1)==0X01)
{
     dataTemp=dataTemp.mid(i-1,dataTemp.length());
    i = 1;

    if(dataTemp.length() <= 5)//不可能有比5小的数据，出错
    {   qDebug()<<"::1";
        return;
    }
    int frameLength = (unsigned char)dataTemp.at(4);
    if(frameLength >= 100)
    {
        //长度有误
       dataTemp = dataTemp.mid(1,dataTemp.length());
        i = 0;
        qDebug()<<"::2";
        continue;

    }

    if((frameLength) < 5)
    {  dataTemp=dataTemp.mid(5,dataTemp.length());
       // qDebug()<<"::3";
        return;
    }

    //数据长度不够
    if(dataTemp.length() <frameLength+7)//长度小于framelength说明数据还没到,等待下一针数据拼接
       {
        //qDebug()<<"::3";
        return;}
        //qDebug()<<"3::3";
    if((unsigned char)dataTemp.at(7+frameLength-1) != 0XAA)
    {
      dataTemp= dataTemp.mid(1,dataTemp.length());
        i=0;
       // qDebug()<<"::4";
        continue;
    }
    //qDebug()<<"4::3";

    QByteArray frame = dataTemp.mid(0,7+frameLength);
    qDebug() << "data:" << dataTemp;
    handleFrame(frame);

   dataTemp= dataTemp.mid(7+frameLength,dataTemp.length());

    i=0;
  }
 }
}

void SerialPortProtocol::handleFrame(const QByteArray &frame)//获取到一个完整数据帧的处理函数
{
 int length = (unsigned char)frame.at(4);
 int nodeId = (unsigned char)frame.at(6);
 int deviceId = (unsigned char)frame.at(7);

 if(deviceId == 0X01)//SHT20温度
 {
      double value = (unsigned char)frame.at(8) + (( double)frame.at(9)/100);
     qDebug() << "温度:" << value;

     QString msg=QString("%1  tem:%2").arg(nodeId==0x10 ? "board1" : "board2").arg(value);

     emit showRequestMessage(msg,1);
     //emit newCo2(value);
     if(nodeId == 0x10)
     DataTemp::getObject()->b1_tem = value;
     else if(nodeId == 0x20)
     DataTemp::getObject()->b2_tem = value;

 }
 else if(deviceId == 0X02)
 {
     double value = (unsigned char)frame.at(8) + (( double)frame.at(9)/100);
     qDebug() << "湿度:" << value;
     QString msg=QString("%1  hum:%2").arg(nodeId==0x10 ? "board1" : "board2").arg(value);
     emit showRequestMessage(msg,2);

     if(nodeId == 0x10)
     DataTemp::getObject()->b1_hum1 = value;
     else if(nodeId == 0x20)
     DataTemp::getObject()->b2_hum1 = value;

 }
 else if(deviceId == 0X03)
 {
     double value = (unsigned char)frame.at(8) + (( double)frame.at(9)/100);
     qDebug() << "光照:" << value;
     QString msg=QString("%1  light:%2").arg(nodeId==0x10 ? "board1" : "board2").arg(value);
     emit showRequestMessage(msg,3);

     if(nodeId == 0x10)
     DataTemp::getObject()->b1_light  = value;
     else if(nodeId == 0x20)
     DataTemp::getObject()->b2_light  = value;
 }
 else if(deviceId == 0X04)
 {
     double value = (unsigned char)frame.at(8) + ((unsigned char)frame.at(9) <<8)+ ((unsigned char)frame.at(10) <<16)
             + ((unsigned char)frame.at(11) <<24)+ ((unsigned char)frame.at(12) <<32)+ ((unsigned char)frame.at(13) <<40)
             + ((unsigned char)frame.at(14) <<48)+ ((unsigned char)frame.at(15) <<56);
     qDebug() << "气压:" << value;
     QString msg=QString("%1  press:%2").arg(nodeId==0x10 ? "board1" : "board2").arg(value);
     emit showRequestMessage(msg,4);

     if(nodeId == 0x10)
     DataTemp::getObject()->b1_press  = value;
     else if(nodeId == 0x20)
     DataTemp::getObject()->b2_press  = value;
 }
 else if(deviceId == 0X05)
 {
     int value = (unsigned char)frame.at(8) + ((unsigned char)frame.at(9) <<8);
     qDebug() << "土壤湿度:" << value;
     QString msg=QString("%1  soilhum2:%2").arg(nodeId==0x10 ? "board1" : "board2").arg(value);
     emit showRequestMessage(msg,5);

     if(nodeId == 0x10)
     DataTemp::getObject()->b1_hum2  = value;
     else if(nodeId == 0x20)
     DataTemp::getObject()->b2_hum2  = value;

 }
 else if(deviceId == 0X06)
 {
     int value = (unsigned char)frame.at(8);
     QString msg;
     if(value == 0X01){
         msg=QString("%1  灌溉水阀:打开").arg(nodeId==0x10 ? "board1" : "board2");
         if(nodeId == 0x10){
         DataTemp::getObject()->b1_water1  = true;
         emit showopenStatus(0x10,0x06,0x01);

         }
         else if(nodeId == 0x20)
         {
         DataTemp::getObject()->b2_water1  = true;
         emit showopenStatus(0x20,0x06,0x01);
          }

     }
     else if(value == 0X00){
          msg=QString("%1  灌溉水阀:关闭").arg(nodeId==0x10 ? "board1" : "board2");
          if(nodeId == 0x10)
          {
          DataTemp::getObject()->b1_water1  = false;
          emit showopenStatus(0x10,0x06,0x00);
          }
          else if(nodeId == 0x20)
          {
          DataTemp::getObject()->b2_water1  = false;
          emit showopenStatus(0x20,0x06,0x00);
          }
     }
     qDebug() << "灌溉水阀:" << value;

     emit showRequestMessage(msg,6);


 }

 else if(deviceId == 0X07)
 {
     int value = (unsigned char)frame.at(8);
     QString msg;
     if(value == 0X01){
         msg=QString("%1  喷洒水阀:打开").arg(nodeId==0x10 ? "board1" : "board2");
         if(nodeId == 0x10)
         {
         DataTemp::getObject()->b1_water2  = true;
         emit showopenStatus(0x10,0x07,0x01);
         }
         else if(nodeId == 0x20){
         DataTemp::getObject()->b2_water2  = true;
         emit showopenStatus(0x20,0x07,0x01);
          }

     }
     else if(value == 0X00){
          msg=QString("%1  喷洒水阀:关闭").arg(nodeId==0x10 ? "board1" : "board2");
          if(nodeId == 0x10)
          {
          DataTemp::getObject()->b1_water2  = false;
          emit showopenStatus(0x10,0x07,0x00);
          }
          else if(nodeId == 0x20)
          {
          DataTemp::getObject()->b2_water2  = false;
          emit showopenStatus(0x20,0x07,0x00);
          }
     }
     qDebug() << "喷洒水阀:" << value;

     emit showRequestMessage(msg,6);


 }



}

